<template>
    <div class="address_r">
      <w-breadcrumb style="font-size: 14px;margin-left:5px" separator="/">
        <w-breadcrumb-item>个人信息管理</w-breadcrumb-item>
        <w-breadcrumb-item>地址簿</w-breadcrumb-item>
      </w-breadcrumb>
      <div>
        <div class="address_btn">
          <w-button
            size="mini"
            type="primary"
            @click="navigateToAddAddressPage()"
            >添加地址</w-button
          >
          <w-button size="mini" type="danger" @click="deleteSelection()"
            >批量删除</w-button
          >
        </div>
        <div class="addressTable_content">
          <w-table
          class="address_table"
          ref="multipleTable"
          :data="
            addressTable.slice(
              (currentPage - 1) * pageSize,
              currentPage * pageSize
            )
          "
          tooltip-effect="dark"
          style="width: 100%"
          @select="handleSelect"
        >
          <w-table-column type="selection" width="60"> </w-table-column>
          <w-table-column
            prop="address"
            label="地址"
            width="300"
            :show-overflow-tooltip="true"
          >
          </w-table-column>
          <w-table-column prop="name" label="姓名" width="160">
          </w-table-column>
          <w-table-column prop="phone" label="联系电话" width="160">
          </w-table-column>
          <w-table-column label="操作">
            <template slot-scope="scope">
              <w-button size="mini" @click="handleEdit(scope.row.id)"
                >编辑</w-button
              >
              <w-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row.id, scope.$index)"
                >删除</w-button
              >
            </template>
          </w-table-column>
        </w-table>
        <w-pagination
          class="receive_bottom"
          @current-change="current_change"
          :current-page="currentPage"
          small
          background
          :page-size="pageSize"
          layout="prev, pager, next"
          :total="addressTable.length"
        >
        </w-pagination>
        </div>
      </div>
    </div>
</template>

<script>
export default {
  name: "Address",
  data() {
    return {
      addressTable: [],
      currentPage: 1,
      pageSize: 6,
      deleteList: [],
      msg: "",
      pageNo: "",
      currentPage_: "",
    };
  },
  methods: {
    current_change(currentPage) {
      this.currentPage = currentPage;
      localStorage.setItem("pageNo", currentPage);
    },
    handleDelete(id, index) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            method: "GET",
            url: "http://1.15.170.222:88/api/manage/address/delete/" + id,
          }).then((response) => {
            console.log(response);
            let code = response.data.code;
            if (code === 0) {
              this.$alert("删除成功", "提示", {
                confirmButtonText: "确定",
              });
              index += (this.currentPage - 1) * this.pageSize;
              var isLast = false;
              if (
                this.currentPage ===
                Math.ceil(this.addressTable.length / this.pageSize)
              ) {
                isLast = true;
              }
              this.addressTable.splice(index, 1);
              if (isLast && this.addressTable.length % this.pageSize === 0) {
                this.currentPage--;
              }
            } else if (code === 500) {
              this.$alert("服务器内部错误", "提示", {
                confirmButtonText: "确定",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(id) {
      this.$router.push({
        name: "bianji",
        query: {
          id: id,
        },
      });
    },
    navigateToAddAddressPage() {
      this.$router.push({
        name: "tianjiadizhi",
      });
    },
    handleSelect(row) {
      this.deleteList = row;
    },
    deleteSelection() {
      if (this.deleteList.length !== 0) {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            for (let obj of this.deleteList) {
              let id = obj.id;
              this.$axios({
                method: "GET",
                url: "http://1.15.170.222:88/api/manage/address/delete/" + id,
              }).then((response) => {
                console.log(response);
                let code = response.data.code;
                if (code === 0) {
                  this.$alert("删除成功", "提示", {
                    confirmButtonText: "确定",
                  });
                } else if (code === 500) {
                  this.$alert("服务器内部错误", "提示", {
                    confirmButtonText: "确定",
                  });
                }
              });
            }
            for (var i = 0; i < 99999999; i++) {
              i++;
            }
            const uid = localStorage.getItem("uid");
            this.$axios({
              method: "GET",
              url: "http://1.15.170.222:88/api/manage/address/user/" + uid,
            }).then((response) => {
              console.log(response);
              this.addressTable = response.data.data;
              console.log("cn");
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$confirm("请选择删除项", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
      }
    },
    getAddress() {
      const uid = localStorage.getItem("uid");
      this.$axios({
        method: "GET",
        url: "http://1.15.170.222:88/api/manage/address/user/" + uid,
        data: {
          uid: uid,
        },
      }).then((response) => {
        console.log(response);
        this.addressTable = response.data.data;
      });
    },
  },
  created(){
    this.getAddress();
    this.currentPage_ = localStorage.getItem('pageNo');
    if(this.currentPage_ != undefined){
      //使用
      this.currentPage = Number(this.currentPage_)
      //丢掉
      localStorage.removeItem("pageNo");
    }
  },
};
</script>

<style>
.address_r {
  padding-top: 40px;
  padding-left: 82px;
  display: flex;
  flex-direction: column;
  width: 1050px;
  height: 680px;
}
.address_table {
  margin-top: 5px;
  margin-bottom: 40px;
  font-size: 14px;
}
.address_btn {
  margin-top: 10px;
  display: flex;
  flex-direction: row;
  justify-content: end;
}
.addressTable_content {
  width: 860px;
  margin-top: 30px;
}
</style>
